package com.wx.controller;

import com.bean.EDelivery;
import com.bean.Message;
import com.bean.User;
import com.mvc.ResponseBody;
import com.service.EDeliverService;
import com.service.UserService;
import com.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: 柏竹
 * @Description: 微信 用户业务管理
 * @Date_Created_in: 2021-05-26 11:32
 * @Modified_By: Sans
 * @Project: 快递e栈
 */
public class UserController {
    
    @ResponseBody("/wx/sendSms.do")
    public String sendSms(HttpServletRequest req, HttpServletResponse resp){
        String userPhone = req.getParameter("phone");
        String code = RandomUtil.getCode()+"";
        boolean login = SMSUtil.login(userPhone , code);
        //后端查看 登录
        System.out.println("code : " + code);
        Message msg = null;
        if (login){
            msg = new Message("验证码发送成功",0,code);
        }else{
            msg = new Message("请检查手机号是否正确",-1,null);
        }
        UserUtil.setLoginSms(req.getSession(),userPhone,code);
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/wx/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        String userPhone = req.getParameter("phone");
        String userCode = req.getParameter("code");
        String sysCode = UserUtil.getLoginSms(req.getSession() , userPhone);
        Message msg = null;
        if (sysCode != null){
            if (userCode.equals(sysCode)){
                // 手机 验证码 一致 ，登录成功
                User user = new User();
                
                //判断 管理员
                if ("admin".equals(userPhone) || "root".equals(userPhone)){
                    msg = new Message(1,null);
                    user.setUser(false);
                }
                //判断 快递员
                List<EDelivery> byPhoneNumber = EDeliverService.findByPhoneNumber(userPhone);
                if (byPhoneNumber == null) {
                    
                    //判断用户
                    List<User> userList = UserService.findUser(UserEnum.USERPHONE.type , userPhone);
                    if (userList == null){
                        msg = new Message("请输入已注册的手机号码",-1,null);
                        return JSONUtil.toJSON(msg);
                    }else{
                        msg = new Message(0,null);
                        user.setUser(true);
                    }
                   
                }else{
                    UserUtil.setUserPhone(req.getSession() , byPhoneNumber.get(0).getPhone());
                    UserUtil.setUserName(req.getSession() , byPhoneNumber.get(0).getName());
                    msg = new Message(1,null);
                    user.setUser(false);
                }
    
                //信息读取成功
                user.setUserphone(userPhone);
                UserUtil.setWxUser(req.getSession(),user);
            }else{
                // 验证码一致 ， 登录失败
                msg = new Message("验证码不一致",-2,null);
            }
        }else {
            msg = new Message("手机未获取短信！",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    
    @ResponseBody ("/wx/userInfo.do")
    public String userInfo(HttpServletRequest req, HttpServletResponse resp){
        User wxUser = UserUtil.getWxUser(req.getSession());
        Message msg  = null;
        if (wxUser.isUser()){
            msg = new Message(wxUser.getUserphone(),0,null);
        }else{
            msg = new Message(wxUser.getUserphone(),1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/wx/getUserHone.do")
    public String getUserHone(HttpServletRequest req, HttpServletResponse resp){
        User wxUser = UserUtil.getWxUser(req.getSession());
        String phone = wxUser.getUserphone();
        List<User> user = UserService.findUser(UserEnum.USERPHONE.type , phone);
        Message msg = null;
        if (user == null){
            
            List<EDelivery> delivery = EDeliverService.findByPhoneNumber(phone);
            if (delivery == null){
                msg = new Message("用户数据异常，请重新登录",-1,null);
            }else{
                msg = new Message(null,0,delivery.get(0));
            }
            
        }else{
            msg = new Message(null,0,user.get(0));
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/wx/newUser.do")
    public String newUser(HttpServletRequest req, HttpServletResponse resp){
        
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        
        
        
        return null;
    }
    
    @ResponseBody ("/wx/logout.do")
    public String logout(HttpServletRequest req, HttpServletResponse resp){
        //销毁Sessionn
        req.getSession().invalidate();
        Message msg = new Message(0,null);
        return JSONUtil.toJSON(msg);
    }
    
    
}
